package com.xuyh.pdf.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xuyh
 * @email 497418972@qq.com
 * @data 2019年3月21日12:39:27
 * @description 分割PDF
 */
public class CutPDF {

    /**
     * 分割PDF文件名称
     */
    private final static String CUT_FILE = "D:\\pdf\\hebing\\shijuan\\1\\1.pdf";

    /**
     * 分割文件输出路径
     */
    private final static String CUT_PDF_PATH = "D:\\pdf\\hebing\\shijuan\\2";

    public static void main(String[] args) {
        createOutputDirectory();
        cutPdf(pdfNo(processPdfNum()));
    }

    public static List<String> processPdfNum() {
        List<String> list = new ArrayList<>();
        list.add("1-43");
        list.add("45-60");

        return list;
    }

    private static void createOutputDirectory() {
        File file = new File(CUT_PDF_PATH);
        if (!file.exists() && file.isDirectory()) {
            file.mkdir();
        }
    }


    public static List<PDFNum> pdfNo(List<String> nums) {
        List<PDFNum> list = new ArrayList<>();
        for (int i = 0; i < nums.size(); i++) {
            PDFNum pdfNum = PDFNum.newInstance(nums.get(i));
            list.add(pdfNum);
        }
        return list;
    }


    public static void cutPdf(List<PDFNum> list) {


        PdfReader reader = null;
        try {
            reader = new PdfReader(CUT_FILE);


            int numberOfPages = reader.getNumberOfPages();
            for (PDFNum pdfNum : list) {
                //如果终止页为0，则设置为最大
                if (pdfNum.getEnd() == 0) {
                    pdfNum.setEnd(numberOfPages);
                }

                String outputFileName = CUT_PDF_PATH + File.separator + pdfNum.getStart() + "-" + pdfNum.getEnd() + "页.pdf";
                FileUtils.createOutputFile(outputFileName);
                FileOutputStream fos = new FileOutputStream(outputFileName);
                Document document = new Document();
                PdfCopy copy = new PdfCopy(document, fos);
                document.open();
                for (int i = pdfNum.getStart(); i <= pdfNum.getEnd(); i++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, i);
                    copy.addPage(page);
                }
                try {
                    if (document != null) {
                        document.close();
                    }
                    if (fos != null) {
                        fos.close();
                    }
                    if (copy != null) {
                        copy.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
}
